/*
 * Created by Jesse Phillips
 * Created on 24-Jan-06
 */
import java.util.*;
/**
 * This class runs a command line interface for use with
 * a personally made LinkedList class.
 * 
 * <p><b>
 * Extra Credit:
 * </b><pre>
 * Attempted extra credit not yet stated.
 * </pre><b>
 * 
 * History:
 * </b><pre>
 * 24-Jan-06: Project started.
 * 26-Jan-06: Created Node Class, LinkedList Class.
 * 28-Jan-06: ListDriver completed.
 * </pre>
 * 
 * @author Jesse Phillips
 * @see "No barrowed code."
 */
public class ListDriver {
	private Scanner userInput;
	private static final int QUIT = 8;
	private LinkedList<Integer> list = null;
	
	/**
	 * Creates a new ListDriver and tells it to run.
	 * 
	 * @param args Arguments passed in are not used.
	 * @return void
	 */
	public static void main(String[] args) {
		(new ListDriver()).run();
	}
	
	/**
	 * Default constricter that prints a welcome to the screen.
	 */
	public ListDriver() {
		userInput = new Scanner(System.in);
		System.out.println("Welcome to ListDriver, where we Drive lists into the wall.");
	}

	/**
	 * Run the program until the user quits
	 * 
	 * @return void
	 */
	public void run() {
		int cmd = 0;
		do {
			System.out.print(menu() + "\n$");
			
			cmd = userInput.nextInt();
			
			System.out.println();
			
			switch(cmd) {
			case 1 : build();
				break;
			case 2 : startOver();
				break;
			case 3 : print(list);
				break;
			case 4 : printRev(list);
				break;
			case 5 : evens();
				break;
			case 6 : everyN();
				break;
			case 7 : removeN();
				break;
			default :
				break;
			}
		}while(cmd < QUIT);
		System.out.println("All list driving done, systems at normal.\n" +
				"(Windows) Please reboot to ensure the stability of your OS.\n" +
				"(Linux) Enjoy your day.");
	}
	
	/**
	 * Remove all instances of any number found in the list. 
	 */
	private void removeN() {
		System.out.print("Enter the number to remove: ");
		Integer N = userInput.nextInt();
		list.removeAll(N);
	}

	/**
	 * Print the content of every Nth element in the list.
	 */
	private void everyN() {
		System.out.print("Print every: ");
		System.out.println(list.everyNth(userInput.nextInt()));
	}

	/**
	 * Create and print out a list of even numbers.
	 */
	private void evens() {
		LinkedList<Integer> newList = new LinkedList();
		while(list.hasNext()) {
			Integer test = list.next();
			if(test%2 == 0)
				newList.add(test);
		}
		
		print(newList);
	}

	/**
	 * Prints the elements in the List in reverse order.
	 */
	private void printRev(LinkedList list) {
		System.out.println(list.stringTo());
	}

	/**
	 * Prints all the elements in the List.
	 */
	private void print(LinkedList list) {
		System.out.println(list);
	}

	/**
	 * Clear the list.
	 */
	private void startOver() {
		list.removeAll();
	}

	/**
	 * Builds an LinkedList of Random Integers based on user size, and seed.
	 */
	private void build() {
		System.out.print("Please enter the size of the list: ");
		int size = userInput.nextInt();
		System.out.print("\nPlease enter the seed value: ");
		long seed = userInput.nextLong();
		System.out.println("\nBuilding LinkedList, please wait . . .");
		
		Random rnd = new Random(seed);
		
		list = new LinkedList<Integer>();
		for(int i = 0; i < size; i++)
			list.add(rnd.nextInt());
	}

	/**
	 * Returns a String of the possible commands. Note: a only one command
	 * is avaible if there is not list yet.
	 * 
	 * @return String of commands.
	 */
	private String menu() {
		if(list != null && !list.isEmpty())
			return "\n1. Build LinkedList \n2. Start over \n3. Print \n4. Print Reverse" +
			"\n5. Generate Evens list \n6. Print every Nth item \n7. Delete N \n>8. Quit";
		else
			return "1. Build LinkedList \n>8. Quit";
	}
}
